// mircoApp/car-manage/special_vehicles/special_vehicles.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "",
    infoList: [],
    page: 1,
    total_page: 0,
    isLoadMore: true,
    emptyImg:app.globalData.imgPath + "images%2Fcar-manage%2FlistNone%402x.png"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.initGet();
    this.getData();
  },
  initGet() {
    this.setData({
      page: 1,
      total_page: 0,
      infoList: [],
      isLoadMore: true
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  applyTap() {
    wx.navigateTo({
      url: '/mircoApp/car-manage/authentication/authentication',
    })
  },
  onChange(e) {
    let name = e.detail.name;
    if (e.detail.index == 0) {
      this.setData({
        active: ""
      })
    } else {
      this.setData({
        active: name
      })
    }
    this.initGet();
    this.getData();
    console.log(name, e.detail)
  },
  async getData() {
    let json = {
      search: {
        status: this.data.active
      }
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.car-manage.frontend.controllers.special.index"),
      data: json
    })
    if (data.result) {
      this.setData({
        infoList: data.data.data,
        total_page: data.data.last_page,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        search: {
          status: this.data.active
        },
        page: this.data.page
      };
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.car-manage.frontend.controllers.special.index"),
        data: json
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          infoList: this.data.infoList.concat(data.data.data)
        })
      } else {
        this.data.isLoadMore = false;
        this.setData({
          isLoadMore: false,
          page: this.data.page - 1
        })
      }
    }
  }
})